<template>
  <div class="regionContainer">
    <LeftModule class="module">
      <CardSlot :title="'能耗总览'" :width="'471px'">
        <div class="energy">
          <div class="energy_left">
            <div class="energy_box">
              <div class="energy_top">
                <div class="energy_top_l">
                  <img src="@/assets/images/ico_power.png" alt="用电" />
                  <div>用电</div>
                </div>
                <div class="energy_top_r">
                  <div>(单位 kwh)</div>
                </div>
              </div>
              <div class="energy_bottom">
                <div class="energy_list">
                  <div>当日</div>
                  <div>19988.00</div>
                </div>
                <div class="energy_list">
                  <div>当月</div>
                  <div>761085.00</div>
                </div>
                <div class="energy_list">
                  <div>当年</div>
                  <div>38670514.00</div>
                </div>
              </div>
            </div>
          </div>
          <div class="energy_right">
            <div class="energy_box">
              <div class="energy_top">
                <div class="energy_top_l">
                  <img src="@/assets/images/ico_powerall.png" alt="用电" />
                  <div>总能耗</div>
                </div>
                <div class="energy_top_r">
                  <div>(单位 tce)</div>
                </div>
              </div>
              <div class="energy_bottom">
                <div class="energy_list">
                  <div>当日</div>
                  <div>35088.00</div>
                </div>
                <div class="energy_list">
                  <div>当月</div>
                  <div>932547.00</div>
                </div>
                <div class="energy_list">
                  <div>当年</div>
                  <div>66677539.00</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </CardSlot>

      <CardSlot :title="'功率电流'" :width="'471px'" :slotShow="true">
        <div id="current" ref="current" class="current" />
        <template #header>
          <el-select
            v-model="currentValue"
            placeholder="选择显示数据"
            size="small"
            style="width: 80px"
            @change="changeCurrent"
          >
            <el-option
              v-for="item in currentOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </CardSlot>

      <CardSlot :title="'监控能耗'" :width="'471px'" :slotShow="true">
        <div id="hourly" class="hourly" />
        <template #header>
          <el-select
            v-model="hourlyValue"
            placeholder="选择显示数据"
            size="small"
            style="width: 80px"
            @change="changeHourly"
          >
            <el-option
              v-for="item in hourlyOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </CardSlot>

      <CardSlot :title="'电能曲线'" :width="'471px'" :slotShow="true">
        <div id="electric" class="electric" />
        <template #header>
          <el-select
            v-model="electricValue"
            placeholder="选择显示数据"
            size="small"
            style="width: 80px"
            @change="changeElectric"
          >
            <el-option
              v-for="item in electricOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </CardSlot>
    </LeftModule>

    <RightModule class="module">
      <CardSlot :title="'用电排名'" :width="'471px'">
        <div class="ranking_mark">
          <div class="ranking_left">
            <div>配电室</div>
            <p>6</p>
          </div>
          <div class="ranking_right">
            <div>用电量</div>
            <p>(单位 kwh)</p>
          </div>
        </div>
        <div id="ranking" class="ranking" />
      </CardSlot>

      <CardSlot :title="'分组能耗'" :width="'471px'" :slotShow="true">
        <div id="grouping" class="grouping" />
        <template #header>
          <el-select
            v-model="groupingValue"
            placeholder="选择显示数据"
            size="small"
            style="width: 80px"
            @change="changeGrouping"
          >
            <el-option
              v-for="item in groupingOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </CardSlot>

      <CardSlot :title="'温湿度状态'" :width="'471px'" :slotShow="true">
        <div id="humiture" class="humiture" />
        <template #header>
          <el-select
            v-model="humitureValue"
            placeholder="选择显示数据"
            size="small"
            style="width: 80px"
            @change="changeHumiture"
          >
            <el-option
              v-for="item in humitureOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </CardSlot>

      <CardSlot :title="'监控设备'" :width="'471px'" :slotShow="true">
        <div class="monitor">
          <div class="monitor_mark">
            <div class="monitor_mark_left">
              <div>摄像头</div>
              <p>13</p>
            </div>
            <div class="monitor_mark_right">
              <div>
                更多设备<img src="@/assets/images/ico_more.png" alt="" />
              </div>
            </div>
          </div>
          <div class="monitor_box">
            <div class="monitor_left">
              <img src="@/assets/images/camera1.jpg" alt="" />
            </div>
            <div class="monitor_right">
              <img src="@/assets/images/camera1.jpg" alt="" />
            </div>
          </div>
        </div>
      </CardSlot>
    </RightModule>
  </div>
</template>

<script setup>
import { ref, onMounted, watch, onUnmounted, nextTick } from "vue";
import { ElSelect, ElOption } from "element-plus";
import * as THREE from "three";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader.js";
import LeftModule from "@/components/sidebar/leftSide.vue";
import RightModule from "@/components/sidebar/rightSide.vue";
import CardSlot from "@/components/cardslot/index.vue";

import * as echarts from "echarts";
let myChartCurrent;
let myChartHourly;
let myChartGrouping;
let myChartRanking;
let myChartHumiture;
let myChartElectric;

let three = null;
let controls = null;

let currentValue = ref("功率");
const currentOptions = [
  {
    value: "功率",
    label: "功率"
  },
  {
    value: "功率1",
    label: "功率1"
  }
];

let hourlyValue = ref("日");
const hourlyOptions = [
  {
    value: "日",
    label: "日"
  },
  {
    value: "月",
    label: "月"
  }
];

// 功率电流
const changeCurrent = e => {
  initCurrent();
};
const initCurrent = () => {
  if (
    myChartCurrent != null &&
    myChartCurrent != "" &&
    myChartCurrent != undefined
  ) {
    myChartCurrent.dispose();
  }

  myChartCurrent = echarts.init(document.getElementById("current"));
  myChartCurrent.clear();
  let option = {
    tooltip: {
      trigger: "axis"
    },
    legend: {
      icon: "rect",
      right: "10px",
      itemWidth: 11,
      itemHeight: 3
    },
    grid: {
      top: "25%",
      left: "3%",
      right: "4%",
      bottom: "3%",
      containLabel: true
    },
    color: ["#30C6DC", "#287CE7"],
    xAxis: {
      type: "category",
      data: ["1日9时", "1日10时", "1日11时", "1日12时", "1日13时"],
      nameTextStyle: {
        color: "#333"
      },
      // boundaryGap: false,
      axisLine: {
        lineStyle: {
          color: "#ccc"
        }
      },
      splitLine: {
        lineStyle: {
          color: "#ccc"
        }
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        color: "#333"
      }
    },
    yAxis: {
      type: "value",
      name: "kw",
      nameTextStyle: {
        color: "#333"
      },
      axisLabel: {
        // formatter: '{value}'
        color: "#333"
      }
    },
    series: [
      {
        name: "人寿南楼",
        type: "line",
        data: [12, 24, 13, 28, 17, 15, 19, 20, 14],
        smooth: true,
        // symbol: 'circle',
        // symbolSize: 8,
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "rgba(120, 254, 240, 1)"
            },
            {
              offset: 0.5,
              color: "rgba(120, 254, 240, 0)"
            }
          ])
        }
      },
      {
        name: "人寿北楼",
        type: "line",
        data: [19, 20, 14, 25, 23, 12, 10, 12, 15],
        smooth: false,
        // symbol: 'circle',
        // symbolSize: 8,
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "rgba(41, 105, 244, 1)"
            },
            {
              offset: 0.7,
              color: "rgba(41, 105, 244, 0)"
            }
          ])
        }
      }
    ]
  };
  option && myChartCurrent.setOption(option);
};

// 逐时能耗
const changeHourly = e => {
  initHourly();
};
const initHourly = () => {
  if (
    myChartHourly != null &&
    myChartHourly != "" &&
    myChartHourly != undefined
  ) {
    myChartHourly.dispose();
  }

  myChartHourly = echarts.init(document.getElementById("hourly"));
  myChartHourly.clear();
  let option = {
    tooltip: {
      trigger: "axis"
    },
    legend: {
      icon: "rect",
      right: "10px",
      itemWidth: 11,
      itemHeight: 3
    },
    grid: {
      top: "25%",
      left: "3%",
      right: "4%",
      bottom: "3%",
      containLabel: true
    },
    color: ["#30C6DC", "#287CE7"],
    xAxis: {
      type: "category",
      data: ["1日9时", "1日10时", "1日11时", "1日12时", "1日13时"],
      nameTextStyle: {
        color: "#333"
      },
      // boundaryGap: false,
      axisLine: {
        lineStyle: {
          color: "#ccc"
        }
      },
      splitLine: {
        lineStyle: {
          color: "#ccc"
        }
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        color: "#333"
      }
    },
    yAxis: {
      type: "value",
      name: "kw",
      nameTextStyle: {
        color: "#333"
      },
      axisLabel: {
        // formatter: '{value}'
        color: "#333"
      }
    },
    series: [
      {
        name: "人寿南楼",
        type: "bar",
        barWidth: 10,
        data: [12, 24, 13, 28, 17, 15, 19, 20, 14],
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "rgba(151, 187, 214, 1)"
            },
            {
              offset: 0.7,
              color: "rgba(22, 218, 203, 1)"
            }
          ])
        }
      },
      {
        name: "人寿北楼",
        type: "bar",
        barWidth: 10,
        data: [19, 20, 14, 25, 23, 12, 10, 12, 15],
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "RGBA(149, 177, 200, 1)"
            },
            {
              offset: 0.7,
              color: "RGBA(2, 168, 254, 1)"
            }
          ])
        }
      }
    ]
  };
  option && myChartHourly.setOption(option);
};

// 分组能耗
let groupingValue = ref("小时");
const groupingOptions = [
  {
    value: "小时",
    label: "小时"
  },
  {
    value: "天",
    label: "天"
  }
];
const changeGrouping = e => {
  initGrouping();
};
const initGrouping = () => {
  if (
    myChartGrouping != null &&
    myChartGrouping != "" &&
    myChartGrouping != undefined
  ) {
    myChartGrouping.dispose();
  }

  myChartGrouping = echarts.init(document.getElementById("grouping"));
  myChartGrouping.clear();
  let option = {
    tooltip: {
      trigger: "axis"
    },
    legend: {
      icon: "rect",
      right: "10px",
      itemWidth: 11,
      itemHeight: 3
    },
    grid: {
      top: "25%",
      left: "3%",
      right: "4%",
      bottom: "3%",
      containLabel: true
    },
    color: ["#30C6DC", "#287CE7"],
    xAxis: {
      type: "category",
      data: ["1日9时", "1日10时", "1日11时", "1日12时", "1日13时"],
      nameTextStyle: {
        color: "#333"
      },
      axisLine: {
        lineStyle: {
          color: "#ccc"
        }
      },
      splitLine: {
        lineStyle: {
          color: "#ccc"
        }
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        color: "#333"
      }
    },
    yAxis: {
      type: "value",
      name: "kw",
      nameTextStyle: {
        color: "#333"
      },
      axisLabel: {
        // formatter: '{value}'
        color: "#333"
      }
    },
    series: [
      {
        name: "南楼空调",
        type: "line",
        data: [12, 24, 13, 28, 17, 15, 19, 20, 14],
        smooth: true,
        // symbol: 'circle',
        // symbolSize: 8,
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "rgba(120, 254, 240, 1)"
            },
            {
              offset: 0.5,
              color: "rgba(120, 254, 240, 0)"
            }
          ])
        }
      },
      {
        name: "北楼空调",
        type: "line",
        data: [19, 20, 14, 25, 23, 12, 10, 12, 15],
        smooth: false,
        // symbol: 'circle',
        // symbolSize: 8,
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "rgba(41, 105, 244, 1)"
            },
            {
              offset: 0.7,
              color: "rgba(41, 105, 244, 0)"
            }
          ])
        }
      },
      {
        name: "南楼空调",
        type: "bar",
        barWidth: 10,
        data: [12, 24, 13, 28, 17, 15, 19, 20, 14],
        z: 10,
        zlevel: 0,
        // itemStyle: { color: {image: PatternImg,repeat: 'repeat'}}}
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "rgba(151, 187, 214, 1)"
            },
            {
              offset: 0.9,
              color: "rgba(22, 218, 203, 0)"
            }
          ])
        }
      },
      {
        name: "北楼空调",
        type: "bar",
        barWidth: 10,
        data: [19, 20, 14, 25, 23, 12, 10, 12, 15],
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "RGBA(149, 177, 200, 1)"
            },
            {
              offset: 0.9,
              color: "RGBA(2, 168, 254, 0)"
            }
          ])
        }
      }
    ]
  };
  option && myChartGrouping.setOption(option);
};

// 用电排名
const initRanking = () => {
  if (
    myChartRanking != null &&
    myChartRanking != "" &&
    myChartRanking != undefined
  ) {
    myChartRanking.dispose();
  }
  myChartRanking = echarts.init(document.getElementById("ranking"));
  myChartRanking.clear();
  let option = {
    tooltip: {
      trigger: "axis"
    },
    legend: {
      right: "10px"
    },
    grid: {
      top: "25%",
      left: "3%",
      right: "4%",
      bottom: "3%",
      containLabel: true
    },
    color: ["#5262FD", "#0759D2", "#30C6DC", "#4DFFD7", "#FFAA31", "#FF541F"],
    xAxis: {
      type: "category",
      data: [
        "南楼1楼",
        "南楼31楼",
        "南楼制冷室",
        "北楼1楼",
        "北楼31楼",
        "北楼制冷室"
      ],
      axisTick: { show: false },
      axisLine: { show: false },
      axisLabel: {
        show: true,
        color: "#333",
        fontSize: 12,
        interval: 0
      }
    },
    yAxis: {
      type: "value",
      splitLine: { show: false },
      axisTick: { show: false },
      axisLine: { show: false },
      axisLabel: { show: false }
    },
    series: [
      {
        type: "pictorialBar",
        barCategoryGap: "-130%",
        data: [23546, 20345, 19363, 17523, 13506, 9537],
        symbol: "path://M0,10 L10,10 C5.5,10 5.5,5 5,0 C4.5,5 4.5,10 0,10 z",
        colorBy: "data",
        itemStyle: {
          opacity: 0.5
        },
        emphasis: {
          itemStyle: {
            opacity: 1
          }
        },
        z: 10,
        label: {
          show: true,
          color: "#000",
          align: "center",
          distance: 5,
          position: "top",
          fontWeight: "bold"
        }
      }
    ]
  };
  option && myChartRanking.setOption(option);
};

// 温湿度曲线
let humitureValue = ref("北中心");
const humitureOptions = [
  {
    value: "北中心",
    label: "北中心"
  },
  {
    value: "南中心",
    label: "南中心"
  }
];
const changeHumiture = e => {
  initHumiture();
};
const initHumiture = () => {
  if (
    myChartHumiture != null &&
    myChartHumiture != "" &&
    myChartHumiture != undefined
  ) {
    myChartHumiture.dispose();
  }
  myChartHumiture = echarts.init(document.getElementById("humiture"));
  myChartHumiture.clear();
  let option = {
    tooltip: {
      trigger: "axis"
    },
    legend: {
      right: "10px"
    },
    grid: {
      top: "25%",
      left: "3%",
      right: "4%",
      bottom: "3%",
      containLabel: true
    },
    color: ["#30C6DC", "#287CE7"],
    xAxis: {
      type: "category",
      data: ["1日9时", "1日10时", "1日11时", "1日12时", "1日13时"],
      nameTextStyle: {
        color: "#333"
      },
      // boundaryGap: false,
      axisLine: {
        lineStyle: {
          color: "#ccc"
        }
      },
      splitLine: {
        lineStyle: {
          color: "#ccc"
        }
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        color: "#333"
      }
    },
    yAxis: {
      type: "value",
      name: humitureValue.value === "湿度" ? "%RH" : "℃"
    },
    series: [
      {
        name: "温度℃",
        type: "line",
        data: [35, 30, 32, 25, 21, 26],
        smooth: true,
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "rgba(120, 254, 240, 1)"
            },
            {
              offset: 0.5,
              color: "rgba(120, 254, 240, 0)"
            }
          ])
        }
      },
      {
        name: "湿度%",
        type: "line",
        data: [45, 40, 42, 35, 31, 36],
        smooth: true,
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "rgba(41, 105, 244, 1)"
            },
            {
              offset: 0.7,
              color: "rgba(41, 105, 244, 0)"
            }
          ])
        }
      }
    ]
  };
  option && myChartHumiture.setOption(option);
};

// 电能曲线
let electricValue = ref("小时");
const electricOptions = [
  {
    value: "小时",
    label: "小时"
  },
  {
    value: "分钟",
    label: "分钟"
  }
];
const changeElectric = e => {
  initElectric();
};
const initElectric = () => {
  if (
    myChartElectric != null &&
    myChartElectric != "" &&
    myChartElectric != undefined
  ) {
    myChartElectric.dispose();
  }
  myChartElectric = echarts.init(document.getElementById("electric"));
  myChartElectric.clear();
  let option = {
    tooltip: {
      trigger: "axis"
    },
    legend: {
      icon: "rect",
      right: "10px",
      itemWidth: 11,
      itemHeight: 3
    },
    grid: {
      top: "20%",
      left: "3%",
      right: "4%",
      bottom: "3%",
      containLabel: true
    },
    color: ["#30C6DC", "#287CE7"],
    xAxis: {
      type: "category",
      data: ["1日9时", "1日10时", "1日11时", "1日12时", "1日13时"],
      nameTextStyle: {
        color: "#333"
      },
      axisLine: {
        lineStyle: {
          color: "#ccc"
        }
      },
      splitLine: {
        lineStyle: {
          color: "#ccc"
        }
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        color: "#333"
      }
    },
    yAxis: {
      type: "value",
      name: "kw",
      nameTextStyle: {
        color: "#333"
      },
      axisLabel: {
        // formatter: '{value}'
        color: "#333"
      }
    },
    series: [
      {
        name: "人寿南楼",
        type: "line",
        data: [12, 24, 13, 28, 17, 15, 19, 20, 14],
        smooth: true,
        // symbol: 'circle',
        // symbolSize: 8,
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "rgba(120, 254, 240, 1)"
            },
            {
              offset: 0.5,
              color: "rgba(120, 254, 240, 0)"
            }
          ])
        }
      },
      {
        name: "人寿北楼",
        type: "line",
        data: [19, 20, 14, 25, 23, 12, 10, 12, 15],
        smooth: false,
        // symbol: 'circle',
        // symbolSize: 8,
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "rgba(41, 105, 244, 1)"
            },
            {
              offset: 0.7,
              color: "rgba(41, 105, 244, 0)"
            }
          ])
        }
      },
      {
        name: "人寿南楼",
        type: "bar",
        barWidth: 10,
        data: [12, 24, 13, 28, 17, 15, 19, 20, 14],
        z: 10,
        zlevel: 0,
        // itemStyle: { color: {image: PatternImg,repeat: 'repeat'}}}
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "rgba(151, 187, 214, 1)"
            },
            {
              offset: 0.9,
              color: "rgba(22, 218, 203, 0)"
            }
          ])
        }
      },
      {
        name: "人寿北楼",
        type: "bar",
        barWidth: 10,
        data: [19, 20, 14, 25, 23, 12, 10, 12, 15],
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "RGBA(149, 177, 200, 1)"
            },
            {
              offset: 0.9,
              color: "RGBA(2, 168, 254, 0)"
            }
          ])
        }
      }
    ]
  };
  option && myChartElectric.setOption(option);
};

// 监控设备
let monitorValue = ref("人寿大厦南区1楼");
const monitorOptions = [
  {
    value: "人寿大厦南区1楼",
    label: "人寿大厦南区1楼"
  },
  {
    value: "人寿大厦南区2楼",
    label: "人寿大厦南区2楼"
  }
];
const changeMonitor = value => {
  monitorValue.value = value;
};

// 模型
const initThree = () => {
  three = window.three;
  controls = window.three.controls;
  // three.renderer.outputEncoding = THREE.sRGBEncoding;
  createLight();
  const GLBLoader = new GLTFLoader();
  const dracoLoader = new DRACOLoader();
  dracoLoader.setDecoderPath("/draco/");
  dracoLoader.setDecoderConfig({ type: "js" });
  GLBLoader.setDRACOLoader(dracoLoader);
  GLBLoader.load(
    "/gltf/f1.gltf",
    gltf => {
      gltf.scene.scale.set(0.08, 0.08, 0.08);
      gltf.scene.position.set(0, -1.5, 0);
      gltf.scene.rotation.set(0, 0, 0);
      changeMaterialEmissive(gltf.scene);
      three.scene.add(gltf.scene);
    },
    progressEvent => {
      const { loaded, total, lengthComputable } = progressEvent;
      if (lengthComputable) {
      } else {
        console.log("f1.gltf加载失败");
      }
    },
    error => {
      console.log("error:", error);
    }
  );

  animate();
};

const createLight = () => {
  const ambientLight = new THREE.AmbientLight(0xffffff, 1);
  three.scene.add(ambientLight);

  // const pointLight = new THREE.PointLight(0xffffff, 0, 0, 10)
  // pointLight.position.set(0, 0, 2)
  // three.scene.add(pointLight)

  // const sphereSize = 1;
  // const pointLightHelper = new THREE.PointLightHelper(pointLight, sphereSize);
  // three.scene.add(pointLightHelper);
};

const changeMaterialEmissive = parent => {
  parent.traverse(function (obj) {
    if (obj instanceof THREE.Mesh) {
      obj.material.emissive = obj.material.color;
      obj.material.opacity = 0.6;
      obj.material.emissiveIntensity = 0;
      obj.material.emissiveMap = obj.material.map;
      obj.material.side = THREE.DoubleSide;
      obj.castShadow = true;
      obj.receiveShadow = true;
      obj.frustumCulled = false;
    }
  });
};

const removeGltf = () => {
  cancelAnimationFrame(animate);
  three.scene.clear();
  three.renderer.content = null;
  three?.renderer.dispose();
  three?.scene.children.forEach(child => {
    three.scene.remove(child);
  });

  // 清理内存中的模型数据
  three?.scene.traverse(child => {
    if (child.isMesh) {
      child.geometry.dispose();
      if (child.material) {
        const material = Array.isArray(child.material)
          ? child.material
          : [child.material];
        material.forEach(mat => mat.dispose());
      }
    }
  });
};

const animate = () => {
  requestAnimationFrame(animate);
  controls.update();
  three?.renderer?.render(three.scene, three.camera);
};

const resizeCharts = () => {
  setTimeout(() => {
    myChartCurrent && myChartCurrent.resize();
    myChartHourly?.resize();
    myChartGrouping?.resize();
    myChartRanking?.resize();
    myChartHumiture?.resize();
    myChartElectric?.resize();
  }, 300);
};

let resizeObserver;

onMounted(() => {
  initCurrent();
  initHourly();
  initGrouping();

  initRanking();
  initHumiture();
  initElectric();

  initThree();

  window.addEventListener("resize", resizeCharts, false);
  const viewElem = document.getElementsByClassName("regionContainer")[0];
  resizeObserver = new ResizeObserver(() => {
    myChartCurrent?.resize();
    myChartHourly?.resize();
    myChartGrouping?.resize();
    myChartRanking?.resize();
    myChartHumiture?.resize();
    myChartElectric?.resize();
  });
  resizeObserver.observe(viewElem);
});

onUnmounted(() => {
  myChartCurrent?.dispose();
  myChartHourly?.dispose();
  myChartGrouping?.dispose();
  myChartRanking?.resize();
  myChartHumiture?.resize();
  myChartElectric?.resize();

  removeGltf();
  if (resizeObserver) [resizeObserver.disconnect()];
  window.removeEventListener("resize", resizeCharts, false);
});
</script>

<style scoped lang="scss">
.module {
  display: flex;
  flex-flow: column nowrap;
  align-items: center;
  justify-content: space-between;

  .cardslot {
    height: 100%;
  }
}

.energy {
  display: flex;
  align-items: center;
  width: 100%;
  height: 100%;

  .energy_left {
    flex: 1;
    border-right: 1px solid #eee;
  }

  .energy_right {
    flex: 1;
  }

  .energy_box {
    padding: 0 15px;

    .energy_top {
      display: flex;
      // align-items: center;
      align-items: last baseline;
      justify-content: space-between;

      .energy_top_l {
        display: flex;
        align-items: center;

        img {
          width: 51px;
          height: 43px;
        }

        div {
          margin-left: 6px;
          font-size: 20px;
          font-weight: bold;
          line-height: 20px;
          color: #333;
        }
      }

      .energy_top_r {
        font-size: 12px;
        line-height: 12px;
        color: #333;
      }
    }

    .energy_bottom {
      padding-top: 20px;

      .energy_list {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding-bottom: 10px;
        padding-left: 14px;

        &:last-child {
          padding-bottom: 0;
        }

        div {
          &:first-child {
            font-size: 14px;
            line-height: 14px;
            color: #333;
          }

          &:last-child {
            font-size: 20px;
            font-weight: bold;
            line-height: 20px;
            color: #0759d2;
          }
        }
      }
    }
  }
}

.current {
  width: 100%;
  height: 100%;
}

.hourly {
  width: 100%;
  height: 100%;
}

.grouping {
  width: 100%;
  height: 100%;
}

.ranking_mark {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;

  .ranking_left,
  .ranking_right {
    display: flex;
    align-items: center;
    font-size: 16px;
    line-height: 16px;

    p {
      padding-left: 8px;
    }
  }

  .ranking_left p {
    color: #2f83e6;
    font-weight: bold;
  }

  .ranking_right p {
    font-size: 12px;
  }
}

.ranking {
  width: 100%;
  height: 100%;
}

.humiture {
  width: 100%;
  height: 100%;
}

.electric {
  width: 100%;
  height: 100%;
}

.monitor {
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  width: 100%;
  height: 100%;

  .monitor_mark {
    width: 100%;
    height: 28px;
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    padding: 0 5px 10px;

    .monitor_mark_left {
      display: flex;
      align-items: center;
      font-size: 16px;
      line-height: 16px;

      p {
        color: #2479ee;
        font-weight: bold;
        padding-left: 5px;
      }
    }

    .monitor_mark_right {
      font-size: 12px;
      line-height: 12px;
      cursor: pointer;
      display: flex;
      align-items: center;

      img {
        padding-left: 5px;
      }
    }
  }

  .monitor_box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;

    .monitor_left,
    .monitor_right {
      flex: 1;
      padding: 0 5px;
      height: 100%;

      img {
        width: 100%;
        height: 100%;
      }
    }
  }
}
</style>
